package com.msl.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.msl.MslApp;
import com.msl.R;
import com.msl.base.BaseActivity;
import com.msl.bean.AbDcodeBean;
import com.msl.scan.BroadcastConfig;
import com.msl.tool.ToastUtils;

import java.io.IOException;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;


public class SCRK117Activity extends BaseActivity {

    private String tag="SCRK117Activity";
    @Bind(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.ivp)
    ImageView ivp;
    @Bind(R.id.login_p)
    EditText loginP;
    @Bind(R.id.tvcon)
    TextView tvcon;
    @Bind(R.id.ll)
    LinearLayout ll;
    String code="";
    AbDcodeBean abDcodeBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrk);
        ButterKnife.bind(this);
        tvTitle.setText("生产入库");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    protected BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(BroadcastConfig.CUSTOM_NAME))
            {
                //ToastUtils.showSingleToast(CGCKActivity.this,  "code:"+intent.getStringExtra("scannerdata") );
                loginP.setText(intent.getStringExtra("scannerdata") );
                waitDialog.show();
                getcode(MslApp.url+"?action=DownloadBill_Detail&cid="+MslApp.sSharedPreferences.getString(MslApp.cidk,"")
                        +"&userID="+MslApp.sSharedPreferences.getInt(MslApp.userID,0)
                        +"&storeID="+MslApp.sSharedPreferences.getInt(MslApp.storeID,0)
                        +"&billcode="+intent.getStringExtra("scannerdata")
                        +"&billType="+"117"
                );
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        initBroadcastReciever();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BroadcastConfig.CUSTOM_NAME);
        registerReceiver(mBroadcastReceiver,intentFilter);
        ll.setVisibility(View.INVISIBLE);
        loginP.setText("");
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mBroadcastReceiver);
    }


    @OnClick({R.id.iv_title_left, R.id.ivp, R.id.ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.ivp:
                code = loginP.getText().toString();
                if(code.length()==0)
                {
                    ToastUtils.showSingleToast(this,"请填写单号");
                    return;
                }
                waitDialog.show();
                getcode(MslApp.url+"?action=DownloadBill_Detail&cid="+MslApp.sSharedPreferences.getString(MslApp.cidk,"")
                        +"&userID="+MslApp.sSharedPreferences.getInt(MslApp.userID,0)
                        +"&storeID="+MslApp.sSharedPreferences.getInt(MslApp.storeID,0)
                        +"&billcode="+code
                        +"&billType="+"117"
                );

//                getcode(MslApp.url+"?action=UpdateCodes&cid="+MslApp.sSharedPreferences.getString(MslApp.cidk,"")
//                        +"&userID="+MslApp.sSharedPreferences.getInt(MslApp.userID,0)
//                        +"&storeID="+MslApp.sSharedPreferences.getInt(MslApp.storeID,0)
//                        +"&billcode="+code
//                        +"&productID="+"111"
//                        +"&codeType="+"111"
//                        +"&codes="+"111"
//                        +"&manualNums="+"111"
//                        +"&subBillCode="+"111"
//                        +"&storeInfo="+"111"
//                        +"&billChangeType="+"111"
//                        +"&transactionCode="+"111"
//                );


                break;
            case R.id.ll:
                Intent intent = new Intent();
                intent.putExtra(MslApp.codedea,abDcodeBean);
                intent.setClass(this,SCRK117Activity1.class);
                startActivity(intent);
                break;
        }
    }

    void getcode(String url)
    {
        try {
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                    waitDialog.dismiss();
                    handler.sendEmptyMessage(1);
                }
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if(response.isSuccessful()){//回调的方法执行在子线程。
                        result = response.body().string();
                        Log.e(tag,"result="+result);
                        waitDialog.dismiss();
                        handler.sendEmptyMessage(0);
                    }
                    else
                    {
                        Log.e(tag,"获取数据失败"+response.code());
                        waitDialog.dismiss();
                    }
                }
            });
        }
        catch (Exception e)
        {
            e.printStackTrace();
            waitDialog.dismiss();
        }
    }

    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case 0:
                    abDcodeBean = gson.fromJson(result, AbDcodeBean.class);
                    if(abDcodeBean.getErrcode()==0 && abDcodeBean.getData().size()!=0)
                    {
                        Intent intent = new Intent();
                        intent.putExtra(MslApp.codedea,abDcodeBean);
                        intent.setClass(SCRK117Activity.this,SCRK117Activity1.class);
                        startActivity(intent);
                    }
                    else
                    {
                        ll.setVisibility(View.INVISIBLE);
                        ToastUtils.showSingleToast(getApplication(),abDcodeBean.getErrmsg());
                    }
                    break;
                case 1:
                    ToastUtils.showSingleToast(getApplication(),"获取订单超时");
                    break;
                default:
                    break;

            }
        }
    };

}
